<template>
  <div>
    <!--     降级规则配置 -->
    <el-row style="text-align: right">
      <el-button type="primary" @click="addUpdataRules" v-show="panePermiss[152]">{{ $t('新增') }}</el-button>
    </el-row>
    <el-row style="margin-top: 10px">
      <Table :page="page" row-key="id" :table-data="tableData" :table-column="tableColumn"
             @handleSizeChange="handleSizeChange"
             @handleCurrentChange="handleCurrentChange">
        <template slot-scope="scope" slot="rulesNum">
          {{ scope.row.rulesType === $t('按天') ? scope.row.rulesNum : '/' }}
        </template>
        <template slot-scope="scope" slot="rulesNum2">
          {{ scope.row.rulesType === $t('按次') ? scope.row.rulesNum : '/' }}
        </template>
        <template slot-scope="scope" slot="handle">
          <el-button type="text" @click="editProduct(scope.row)" v-show="panePermiss[154]">{{
              $t('编辑')
            }}
          </el-button>
          <el-button type="text" @click="deleteProduct(scope.row)" v-show="panePermiss[1059]">{{
              $t('删除')
            }}
          </el-button>
        </template>
      </Table>
    </el-row>
    <Dialog :visible.sync="visible.isShow" @confirm="visibleConfirm" @cancel="visible.isShow = false"
            :title="visible.title" @close="clearForm" :confirm-loading="confirmLoading">
      <el-form :model="form" ref="form" label-width="150px" :rules="rulesForm">
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item :label="$t('降级规则名称')" prop="rulesName">
              <el-input v-model="form.rulesName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('降级方式')" prop="rulesType">
              <el-select style="width: 100%" v-model="form.rulesType" @change="changeType">
                <el-option :label="$t('按天')" :value="1"></el-option>
                <el-option :label="$t('按次')" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="form.rulesType === 1 ? $t('逾期天数') : $t('逾期次数') " prop="rulesNum">
              <el-input v-model.number="form.rulesNum" oninput="value = value.replace(/[^\d]/g, '')"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('下调金额')" prop="amount">
              <el-input v-model="form.amount" oninput="value = value.replace(/[^\d.]/g, '')"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('描述')" prop="describe">
              <el-input type="textarea" v-model="form.describe"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </Dialog>
  </div>
</template>

<script>
import {dateFormat, deepClone} from "@/utils/tools";
import {addUserLoanRules,userLoanRulesDelete,userLoanRulesList,updateUserLoanRules} from '@/api/product'
export default {
  props: ["panePermiss"],
  data() {
    const validator = (rule, value, callBack) => {
      if (parseFloat(value) === 0) {
        return callBack(new Error(this.$t('不能为零')))
      }
      return callBack()
    }
    return {
      tableData: [],
      tableColumn: [
        {label: this.$t('降级规则名称'), prop: 'rulesName', align: 'center',},
        {label: this.$t('降级方式'), prop: 'rulesType', align: 'center',},
        {label: this.$t('逾期天数'), prop: 'rulesNum', slot: 'rulesNum', align: 'center',},
        {label: this.$t('逾期次数'), prop: 'rulesNum2', slot: 'rulesNum2', align: 'center',},
        {label: this.$t('下调金额'), prop: 'amount', align: 'center',},
        {label: this.$t('创建时间'), prop: 'createTime', align: 'center', width: 150},
        {label: this.$t('更新时间'), prop: 'updateTime', align: 'center', width: 150},
        {label: this.$t('操作'), slot: 'handle', align: 'center', width: 100},
      ],
      form: {},
      rulesForm: {
        rulesType: [
          {required: true, message: this.$t('请选择'), trigger: 'change'},
        ],
        rulesName: [
          {required: true, message: this.$t('不能为空')},
        ],
        rulesNum: [
          {required: true, message: this.$t('不能为空')},
          { required: true, validator: validator }
        ],
        amount: [
          {required: true, message: this.$t('不能为空')},
          { required: true, validator: validator }
        ],
      },
      visible: {
        isShow: false,
        type: '',
        title: '',
      },
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        type: 3,
        productId: this.$route.query.productId,
      },
      confirmLoading: false,
      anthObj: {}
    }
  },
  created() {
  },
  mounted() {
    this.getTableData()
  },
  methods: {
    async getTableData() {
      const params = {
        ...this.page,
      }
      const res = await userLoanRulesList( params)
      const {list,total} = res.data
      if (res.code === 1) {
        this.tableData = list.map(item => {
          item.status = item.status === 1
          item.rulesType = item.rulesType === 1 ? this.$t('按天') : this.$t('按次')
          return item
        })
        this.page = Object.assign({}, this.page, {
          total: total
        })
      }
    },
    clearForm() {
      this.form = {}
      this.$refs.form.resetFields()
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      this.page.pageNum = val
      this.getTableData()
    },
    async changeStatus(row) {
      const res = await this.$post('loanProduct/updateUserLoanRulesStatus', {
        id: row.id,
        status: row.status ? 2 : 1,
        productId: this.$route.query.productId,
      })
      if (res.code === 1) {
        this.$message.success(this.$t('修改成功'))
        this.getTableData()
      }
    },
    async deleteProduct({id}) {
      this.$confirm(this.$t('确定删除?'), this.$t('提示'), {
        type: 'warning'
      }).then(async () => {
        const res = await userLoanRulesDelete({id})
        if (res.code === 1) {
          this.$message({
            type: 'success',
            message: this.$t('删除成功')
          });
          this.getTableData()
        }
      }).catch(() => {
      });
    },
    changeType() {
      if (this.form.amount) {
        this.form.amount = null
      }
    },
    async addUpdataRules() {
      this.visible = {
        isShow: true,
        type: 'add',
        title: this.$t('添加降级规则')
      }
    },
    async editProduct(row) {
      const tableRow = deepClone(row)
      this.form = Object.assign({}, tableRow, {
        rulesType: tableRow.rulesType === this.$t('按天') ? 1 : 2
      })
      this.visible = {
        isShow: true,
        type: 'edit',
        title: this.$t('编辑降级规则')
      }
    },
    async visibleConfirm() {
      this.$refs.form.validate(async v => {
        if (v) {
          let message = ''
          let api
          this.confirmLoading = true
          const params = Object.assign({}, this.form, {
            amount: parseFloat(this.form.amount),
            productId: this.$route.query.productId,
            type: 3,
          })
          if (this.visible.type === 'add') {
            api = addUserLoanRules(params)
            message = this.$t('新增成功')
          } else {
            api = updateUserLoanRules(params)
            message = this.$t('修改成功')
          }
          const res = await api
          if (res.code === 1) {
            this.$message.success(message)
            this.clearForm()
            this.confirmLoading = false
            this.visible.isShow = false
            this.getTableData()
          } else {
            this.confirmLoading = false
          }
        }
      })

    },

  }
}
</script>

<style scoped>

</style>
